package com.liliudong.rommath.api.sys.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.liliudong.rommath.biz.sys.param.SysRoleAddParam;
import com.liliudong.rommath.biz.sys.param.SysRoleModifyParam;
import com.liliudong.rommath.biz.sys.param.SysRolePageParam;
import com.liliudong.rommath.biz.sys.result.SysRoleListResult;
import com.liliudong.rommath.biz.sys.result.SysRolePageResult;
import com.liliudong.rommath.biz.sys.service.SysRoleService;
import com.liliudong.rommath.common.R;
import com.mybatisflex.core.paginate.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.liliudong.rommath.biz.sys.entity.table.SysRoleTableDef.SYS_ROLE;

/**
 * 系统角色 控制层。
 *
 * @author liudong.li
 * @since 1.0.0
 */
@RestController
@RequestMapping("/sys/role")
@RequiredArgsConstructor
public class SysRoleController {

    private final SysRoleService sysRoleService;

    /**
     * 添加
     *
     * @param param param
     * @return {@link R }<{@link Void }>
     */
    @SaCheckPermission("sys:role:add")
    @PostMapping
    public R<Void> add(@RequestBody SysRoleAddParam param) {
        sysRoleService.add(param);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param id 身份证件
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:role:delete")
    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable Long id) {
        sysRoleService.deleteById(id);
        return R.ok();
    }

    /**
     * 修改
     *
     * @param param param
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:role:modify")
    @PutMapping
    public R<?> modify(@RequestBody SysRoleModifyParam param) {
        sysRoleService.modify(param);
        return R.ok();
    }

    /**
     * 分页
     *
     * @param param param
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:role:query")
    @GetMapping("/page")
    public R<Page<SysRolePageResult>> page(SysRolePageParam param) {
        Page<SysRolePageResult> page = sysRoleService.queryPage(param);
        return R.ok(page);
    }

    /**
     * 列表
     *
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:role:query")
    @GetMapping("/list")
    public R<?> list() {
        List<SysRoleListResult> result = sysRoleService.queryChainIgnoreDataPermission()
                .select(SYS_ROLE.ID, SYS_ROLE.NAME)
                .listAs(SysRoleListResult.class);
        return R.ok(result);
    }

}